package com.yby6.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.yby6.common.utils.StringUtils;
import com.yby6.common.utils.context.IdWorker;
import com.yby6.common.core.domain.SysUser;
import com.yby6.common.core.domain.SysUserRole;
import com.yby6.system.mapper.SysUserMapper;
import com.yby6.system.mapper.SysUserRoleMapper;
import com.yby6.system.service.SysUserService;
import com.yby6.system.vo.SysUserVo;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2021/5/13
 *
 * @author Yang Shuai
 * @since JDK 1.8
 **/

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public int deleteUserByIds (Long[] ids) {
        return sysUserMapper.deleteByPrimaryKey(ids);
    }

    /**
     * 新增用户与角色信息
     *
     * @param record
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert (SysUserVo record) {
        SysUser sysUser = new SysUser();
        record.setId(idWorker.nextId());
        BeanUtils.copyProperties(record, sysUser);
        int insert = sysUserMapper.insert(sysUser);
        // 新增用户与角色管理: 如果该用户下拥有角色ID 那么新增到 用户->角色关系表当中
        insertUserRole(record);
        return insert;
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole (SysUserVo sysUserVo) {
        Long[] roles = sysUserVo.getRoleIds();
        if (StringUtils.isNotNull(roles)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roles) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(sysUserVo.getId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0) {
                sysUserRoleMapper.batchUserRole(list);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update (SysUserVo record) {
        SysUser sysUser = new SysUser();
        // 拷贝数据
        BeanUtils.copyProperties(record, sysUser);
        // 删除用户与角色关联
        sysUserRoleMapper.deleteUserRoleByUserId(sysUser.getId());
        // 新增用户与角色关联
        insertUserRole(record);
        return sysUserMapper.updateByPrimaryKey(sysUser);
    }
//
//    @Override
//    public SysUser getByUsername (UsernamePasswordToken usernamePasswordToken) {
//        return sysUserMapper.getUserNameByPassword(usernamePasswordToken.getUsername(), String.valueOf(usernamePasswordToken.getPassword()));
//    }

    @Override
    public int enabled (Long id, Long status) {
        return this.sysUserMapper.enabled(id, status);
    }

    @Override
    public List<SysUser> list (SysUser sysUser) {
        return this.sysUserMapper.list(sysUser);
    }

    @Override
    public SysUserVo getUserById (Long id) {
        return this.sysUserMapper.getById(id);
    }

    /**
     * 登陆成功更新登陆时间
     *
     * @param sysUser
     */
    @Override
    public void updateByLoginTime (SysUser sysUser) {
        this.sysUserMapper.updateByPrimaryKey(sysUser);
    }

    @Override
    public SysUser getUserByOpenId (String openid) {
        return this.sysUserMapper.getUserByOpenId(openid);
    }

    @Override
    public int updateByOpenId (SysUserVo sysUserVo) {
        return this.sysUserMapper.updateByOpenId(sysUserVo);
    }

    @Override
    public SysUser getUserInfoByFaceId (String faceId) {
        return this.sysUserMapper.getUserInfoByFaceId(faceId);
    }

    @Override
    public List<String> selectRolesByUserId (Long id) {
        return this.sysUserMapper.selectRolesByUserId(id);
    }
}

